import Vue from 'vue'
/**
 * 表单输入框文本标签多语言自动切换
 */
Vue.directive('field-lbl', {
    bind(el, binding, vnode) {
        const field = binding.value
        if (field && vnode.context.$i18n.locale !== 'zhCN') {
            let moduleInfo = vnode.context.moduleInfo
            if (!moduleInfo) {
                moduleInfo = vnode.context.$vnode.context.moduleInfo
            }

            const expression = moduleInfo.moduleCode + '.fields.' + field
            if (vnode.context.$te(expression)) {
                el.innerHTML = vnode.context.$t(expression)
            } else {
                el.innerHTML = field
            }
        }
    },
    inserted(el, binding, vnode) {
        const field = binding.value
        if (field && vnode.context.$i18n.locale !== 'zhCN') {
            let moduleInfo = vnode.context.moduleInfo
            if (!moduleInfo) {
                moduleInfo = vnode.context.$vnode.context.moduleInfo
            }

            const expression = moduleInfo.moduleCode + '.fields.' + field
            if (vnode.context.$te(expression)) {
                el.innerHTML = vnode.context.$t(expression)
            } else {
                el.innerHTML = field
            }
        }
    }
})
